package main.controller;

import main.model.Video;
import main.service.VideoService;
import main.view.FrontView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FrontController {
    private FrontView view;
    private VideoService videoService;

    public FrontController(FrontView view) {
        this.view = view;
        this.videoService = VideoService.getInstance();

        // 初始化视频列表
        updateVideoList();

        // 添加事件监听
        view.addPlayButtonListener(new PlayButtonListener());
    }

    private void updateVideoList() {
        view.setVideoList(videoService.getAllVideos());
    }

    class PlayButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            Video selectedVideo = view.getSelectedVideo();
            if (selectedVideo != null) {
                view.playVideo(selectedVideo.getFilePath());
            } else {
                JOptionPane.showMessageDialog(view, "请选择一个视频", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}